﻿<?php
@session_start();
require_once("../../class/mysql.class.php");
if (!isset($_GET['id_entree'])) {
	header('location:../../list_entree.php?action=error');  
}else{
$obj = new Mysql ();
      $requte="DELETE FROM `st_attr_entree` WHERE `id_entree`='".$_GET["id_entree"]."'";
      $resultat=mysql_query($requte);

      //get qt from article 
      $s = "SELECT Quantite from st_article WHERE id_article = ".$_GET['id_article'];
      echo '<br> SELECT QT => '.$s.'<br>';
      $r = mysql_query($s);
      $QT_article = mysql_fetch_array($r);
      echo '<br> la qt reel = '.$_GET['qt'].'<br>';
      echo '<br> la qt q supprimer du stock = '.$QT_article['Quantite'].'<br>';
      $newQt = (int)$QT_article['Quantite'] - (int)$_GET['qt'];
      //update to new qt
      $up = "UPDATE st_article SET Quantite = ".$newQt." WHERE id_article = ".$_GET['id_article'];
      echo '<br> UPDATE => '.$up.'<br>';
      mysql_query($up);


      //get all entry by id and test if num_rows is null to delete the global entry
      //get qt from article 
      /*$ss = "SELECT * from st_attr_entree WHERE id_entree = ".$_GET['id_entree'];
      echo '<br> SELECT QT => '.$ss.'<br>';
      $rr = mysql_query($ss);
      $nb = mysql_num_rows($rr);
      echo $nb;
      if($nb == '0'){
      	//aucune entree -> delete entry
      	$requte_del="DELETE FROM `st_entree` WHERE `id_article`='".$_GET["id_article"]."'";
      	echo $requte_del;
      	mysql_query($requte_del);
      }*/

      //redirect
      header('location:../../list_entree.php?action=deleted&page=list_entree');  
}
	  
?>